package com.meiyu.live.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.param.WithdrawParams;
import com.meiyu.live.net.response.CoinsResponse;
import com.meiyu.live.popj.WithdrawInfo;
import com.meiyu.live.util.ToastUtil;
import com.meiyu.live.widget.NetLoadingDailog;

/**
 * 我的账户
 * Created by zbl on 2016/9/22.
 */
public class MyCoinsActivity extends BaseActivity implements View.OnClickListener {

    protected NetLoadingDailog dialog_loading;
    private View btn_back;
    private TextView tv_mycoins, tv_canwithdraw, tv_amount, tv_withDraw_log, btn_getcash;
    private EditText et_coins;

    private long myCoins, gold_coins;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycoins);
        init();
        getData();
    }

    private void init() {
        dialog_loading = new NetLoadingDailog(this);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_getcash = (TextView) findViewById(R.id.btn_getcash);
        btn_getcash.setOnClickListener(this);
        tv_mycoins = (TextView) findViewById(R.id.tv_mycoins);
        tv_amount = (TextView) findViewById(R.id.tv_amount);
        tv_withDraw_log = (TextView) findViewById(R.id.tv_withDraw_log);
        tv_withDraw_log.setOnClickListener(this);
        et_coins = (EditText) findViewById(R.id.et_coins);
        et_coins.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s.toString())) {
//                    BigDecimal bg = new BigDecimal(Integer.parseInt(s.toString()) / (double) 100);
//                    tv_amount.setText(bg.setScale(2, RoundingMode.HALF_UP) + "元");
                    try {
                        tv_amount.setText(String.format("%.2f", Long.parseLong(s.toString()) / (double) 100) + "元");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tv_canwithdraw = (TextView) findViewById(R.id.tv_canwithdraw);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        } else if (view == btn_getcash) {
            postData();

        } else if (view == tv_withDraw_log) {
            Intent intent = new Intent(this, WithdrawActivity.class);
            startActivity(intent);
        }
    }

    private void getData() {
        dialog_loading.loading();
        BaseBodyParams b = new BaseBodyParams();
        b.setUrl(Constant.UrlParams.MY_COINS);
        request(b, CoinsResponse.class, new Response.Listener<CoinsResponse>() {
            @Override
            public void onResponse(CoinsResponse coinsResponse) {
                dialog_loading.dismissDialog();
                if (coinsResponse != null) {
                    myCoins = coinsResponse.getCarrots();
                    gold_coins = coinsResponse.getGold_coins();
                    tv_mycoins.setText(myCoins + "");
                    tv_canwithdraw.setText(gold_coins + "");
                }
            }
        });
    }

    private void postData() {
        if (TextUtils.isEmpty(et_coins.getText())) {
            ToastUtil.show("请输入提现鱼粮数量");
            return;
        } else if (Long.parseLong(et_coins.getText().toString()) > gold_coins) {
            ToastUtil.show("鱼粮余额不足");
            return;
        }
        dialog_loading.loading();
        request(new WithdrawParams((long) Double.parseDouble(tv_amount.getText().toString().replace("元", "")) * 100, 0, WithdrawParams.ACTION_APPLY, WithdrawParams.ACCOUNT_TYPE_ALIPAY), WithdrawInfo.class, new Response.Listener<WithdrawInfo>() {
            @Override
            public void onResponse(WithdrawInfo withdrawInfo) {
                dialog_loading.dismissDialog();
                if (withdrawInfo.getStatusCode() != 6) {
                    ToastUtil.show(withdrawInfo.getMsg());
                } else if (withdrawInfo.getStatus() == 0){
                    ToastUtil.show("提交成功，请等待审核！");
                }
            }
        });
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onError() {
        super.onError();
        dialog_loading.dismissDialog();
    }
}
